package service.impl;

import entity.AcademyEntity;
import entity.ClassEntity;
import entity.SpecialtyEntity;
import entity.StudentEntity;
import dao.StudentDao;
import dao.impl.StudentDaoImpl;
import dto.ChooseClassDTO;
import service.AcademyService;
import service.ClassService;
import service.SpecialtyService;
import service.StudentService;
import util.UUIDUtil;
import vo.GetStudentVO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao = new StudentDaoImpl();

    private final AcademyService academyService = new AcademyServiceImpl();

    private final SpecialtyService specialtyService = new SpecialtyServiceImpl();
    private final ClassService classService = new ClassServiceImpl();

    @Override
    public List<GetStudentVO> getAll(String name) {
        List<Map<String, Object>> mapList = studentDao.getAll(name);
        List<StudentEntity> studentEntityList = StudentEntity.mapListToEntityList(mapList);
        return studentEntityList.stream().map(student -> {
            GetStudentVO vo = new GetStudentVO();
            vo.setId(student.getId());
            vo.setStudentId(student.getStudentId());
            if (student.getAcademyId() != null) {
                AcademyEntity academy = academyService.getAcademyInfoById(student.getAcademyId());
                vo.setAcademyId(academy.getId());
                vo.setAcademyName(academy.getAcademyName());
            }
            if (student.getSpecialtyId() != null) {
                SpecialtyEntity specialty = specialtyService.getSpecialtyInfoById(student.getSpecialtyId());
                vo.setSpecialtyId(specialty.getId());
                vo.setSpecialtyName(specialty.getSpecialtyName());
            }
            if (student.getClassId() != null) {
                ClassEntity classEntity = classService.getClassInfoById(student.getClassId());
                vo.setClassId(classEntity.getId());
                vo.setClassName(classEntity.getClassName());
            }

            vo.setName(student.getName());
            vo.setGender(student.getGender());
            vo.setAge(student.getAge());
            vo.setGrade(student.getGrade());
            vo.setDegree(student.getDegree());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addStudent(StudentEntity student) {
        student.setId(UUIDUtil.getUUID());
        studentDao.addStudent(student);
    }

    @Override
    public void deleteStudent(String id) {
        studentDao.deleteStudent(id);
    }

    @Override
    public void updateStudent(StudentEntity student) {
        studentDao.updateStudent(student);
    }

    @Override
    public void batchDeleteStudent(List<String> ids) {
        studentDao.batchDeleteStudent(ids);
    }

    @Override
    public int chooseClass(ChooseClassDTO dto) {
        return studentDao.chooseClass(dto);
    }

}
